<template>
  <div class="course-video">
      <el-card>
          <div slot="header">
              课程相关信息
          </div>
          <el-form>
              <el-form-item label="视频上传">
                  <input type="file" ref="video-file">
              </el-form-item>
              <el-form-item label="封面上传">
                  <input type="file" ref="image-file">
              </el-form-item>
              <el-form-item>
                  <el-button type="primary"
                  @click="handleUploader">开始上传</el-button>
                  <el-button @click="$router.push({
                      name: 'course-section',
                      params: {
                          courseId
                          }})">返回</el-button>
              </el-form-item>
          </el-form>
      </el-card>
  </div>
</template>

<script>
/* eslint-disable */
import { uploadImage, uploadVideo, aliyunTransCode, aliyunTransCodePercent } from '@/services/aliyun'
export default {
  name: 'courseVideo',
  props: {
    courseId: {
      type: [Number, String],
      require: true
    }
  },
  data () {
    return {
      imageURL: '',
      uploader: null,
      videoId: null,
      aliyun: {
        id: 0,
        courseId: 0,
        sectionId: 0,
        lessonId: 0,
        channel: 0,
        mediaType: 0,
        coverImageUrl: '',
        duration: '',
        fileId: '',
        fileUrl: '',
        fileEdk: '',
        fileSize: 0,
        fileName: '',
        fileDk: '',
        createTime: '',
        updateTime: '',
        status: 0,
        isDel: true,
        lastOperatorId: 0,
        durationNum: 0
      }
    }
  },
  created () {
    this.initUploader()
  },
  methods: {
    handleUploader () {
      const videoFile = this.$refs['video-file'].files[0]
      const imageFile = this.$refs['image-file'].files[0]
      const uploader = this.uploader
      uploader.addFile(imageFile)
      uploader.addFile(videoFile)
      uploader.startUpload()
    },
    initUploader () {
      this.uploader = new AliyunUpload.Vod({
        // 阿里账号ID，必须有值
        userId: '1618139964448548',
        // 上传到视频点播的地域，默认值为'cn-shanghai'，//eu-central-1，ap-southeast-1
        region: '',
        // 分片大小默认1 MB，不能小于100 KB
        partSize: 1048576,
        // 并行上传分片个数，默认5
        parallel: 5,
        // 网络原因失败时，重新上传次数，默认为3
        retryCount: 3,
        // 网络原因失败时，重新上传间隔时间，默认为2秒
        retryDuration: 2,
        // 开始上传
        onUploadstarted: async (uploadInfo) => {
          let uploadAddressAndAuth = null
          console.log(uploadInfo)
          if (uploadInfo.isImage) {
            const { data } = await uploadImage()
            if (data.code === '000000') {
              uploadAddressAndAuth = data.data
              this.imageURL = uploadAddressAndAuth.imageURL
            }
          } else {
            const { data } = await uploadVideo({
              fileName: uploadInfo.file.name,
              imageUrl: this.imageURL
            })
            if (data.code === '000000') {
              uploadAddressAndAuth = data.data
              this.videoId = data.data.videoId
            }
            this.uploader.setUploadAuthAndAddress(uploadInfo,
              uploadAddressAndAuth.uploadAuth,
              uploadAddressAndAuth.uploadAddress,
              uploadAddressAndAuth.imageId || uploadAddressAndAuth.videoId
            )
          }
        },
        // 文件上传成功
        onUploadSucceed: function (uploadInfo) {
        },
        // 文件上传失败
        onUploadFailed: function (uploadInfo, code, message) {
        },
        // 文件上传进度，单位：字节
        onUploadProgress: function (uploadInfo, totalSize, loadedPercent) {
        },
        // 上传凭证或STS token超时
        onUploadTokenExpired: function (uploadInfo) {
        },
        // 全部文件上传结束
        onUploadEnd: async (uploadInfo) => {
          await aliyunTransCode({
            lessonId: this.$route.query.lessonId,
            coverImageUrl: this.imageURL,
            fileId: this.videoId,
            fileName: this.$refs['video-file'].file[0].name
          })
          if (data.code === '000000') {
            const timer = setInterval(async () => {
              const { data: data1 } = await getAliyunTranscodePercent(lessonId)
              if (data1.code === '000000') {
                console.log(data1)
                if (data1.data === 100) {
                  this.$message.success('视频转码成功')
                  clearInterval(timer)
                }
              }
            }, 1000)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
